package com.itheima.Chat;

import java.util.ArrayList;
import java.util.Scanner;

/*
    运行系统
 */
public class System01 {
    public static void main(String[] args) {
        //用户列表
        ArrayList<User> user = new ArrayList<>();
        //管理员列表
        ArrayList<Admin> admin = new ArrayList<>();
        //好友列表
        ArrayList<Friend> friends = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        String count1;
        lo:
        while (true) {
            System.out.println("----------欢迎使用跟谁学在线交友----------");
            System.out.println("1.用户注册");
            System.out.println("2.用户登录");
            System.out.println("3.管理员登录");
            System.out.println("4.退出使用");
            count1 = sc.next();
            switch (count1) {
                case "1":
                    login(user);
                    break;
                case "2":
                    enter(user, friends);
                    break;
                case "3":
                    enterAdmin(user);
                    break;
                case "4":
                    System.out.println("感谢您的使用!");
                    break lo;
                default:
                    System.out.println("您的输入有误请重新输入!");
            }
        }
        /*
        for (int i = 0; i < user.size(); i++) {
            User u = user.get(i);
            System.out.println(u.getId());
        }
        */

    }

    //注册系统
    public static void login(ArrayList<User> user) {
        Scanner sc = new Scanner(System.in);
        //输入注册信息
        User idd = new User();
        String id;
        String passWord;
        lo:
        while (true) {
            System.out.println("请输入账号:(账号为8位数字)");
            id = sc.next();
            char[] chs = id.toCharArray();
            if (id.length() != 8) {
                System.out.println("账号为8位数字,请重新输入!");
                continue;
            } else if (id.length() == 8) {
                for (int i = 0; i < chs.length; i++) {
                    if (chs[i] > '9' || chs[i] < '0') {
                        System.out.println("账号为8位数字,请重新输入!");
                        continue lo;
                    }
                }
                for (int i = 0; i < user.size(); i++) {
                    idd = user.get(i);
                    if (id.equals(idd.getId())) {
                        System.out.println("用户已存在请重新输入!");
                        continue lo;
                    }
                }
            }
            break;
        }
        loo:
        while (true) {
            System.out.println("请输入密码:(密码可由数字和字母自由组合,不得少于8位,不得超过20位)");
            passWord = sc.next();
            char[] ch = passWord.toCharArray();
            if (passWord.length() < 8) {
                System.out.println("密码不得少于8位!");
                continue;
            } else if (passWord.length() > 20) {
                System.out.println("密码不得多于20位!");
                continue;
            } else {
                for (int i = 0; i < ch.length; i++) {
                    if (ch[i] < '0' || (ch[i] > '9' && ch[i] < 'A') || (ch[i] > 'Z' && ch[i] < 'a') || ch[i] > 'z') {
                        System.out.println("存在非法字符请重新输入!");
                        continue loo;
                    }
                }
                break;
            }
        }
        System.out.println("请输入您的验证信息:");
        String VM = sc.next();
        System.out.println("添加成功!");
        User u = new User(id, passWord, VM);
        user.add(u);
        /*
        for (int i = 0; i < user.size(); i++) {
            User u1 = user.get(i);
            System.out.println(u1.getId());
        }
        */
    }

    //用户登录系统
    public static void enter(ArrayList<User> user, ArrayList<Friend> friends) {
        Scanner sc = new Scanner(System.in);
        String id;
        String passWord;
        lo:
        while (true) {
            System.out.println("请输入用户名:");
            id = sc.next();
            System.out.println("请输入登陆密码:");
            passWord = sc.next();
            User u = new User();
            for (int i = 0; i < user.size(); i++) {
                u = user.get(i);
                if (id.equals(u.getId()) && passWord.equals(u.getPassWord())) {
                    System.out.println("登陆成功!");
                    userInterface(u, friends, user);
                    break lo;
                } else if (i == user.size() - 1) {
                    System.out.println("用户名或密码错误请重新输入!");
                    continue lo;
                }
            }
        }
    }

    //建立用户界面
    public static void userInterface(User u, ArrayList<Friend> friends, ArrayList<User> user) {
        Scanner sc = new Scanner(System.in);
        String id = u.getId();
        String op;
        //界面展示
        lo:
        while (true) {
            System.out.println("欢迎" + u.getVM() + "使用跟谁学在线交友!");
            System.out.println("1.添加好友");
            System.out.println("2.删除好友");
            System.out.println("3.修改好友昵称:");
            System.out.println("4.查看好友信息:");
            System.out.println("5.退出");
            op = sc.next();
            switch (op) {
                case "1":
                    addFriend(u, id, friends, user);
                    break;
                case "2":
                    delFriend(id, friends);
                    break;
                case "3":
                    alterFriend(id, friends);
                    break;
                case "4":
                    checkFriend(id, friends);
                    break;
                case "5":
                    System.out.println("感谢您的使用!");
                    break lo;
                default:
                    System.out.println("您的输入有误请重新输入!");
                    continue lo;
            }
        }
    }

    //建立用户添加好友界面
    public static void addFriend(User u, String id, ArrayList<Friend> friends, ArrayList<User> user) {
        Scanner sc = new Scanner(System.in);
        User u1 = new User();
        Friend friend = new Friend();
        String friendid;
        String VM = null;
        String name;
        int index = 0;//记录friendid账号对应的索引值
        lo:
        while (true) {
            //引导用户添加好友信息
            System.out.println("请输入好友的账号:(输入-1以结束)");
            friendid = sc.next();
            if ("-1".equals(friendid)) {
                userInterface(u, friends, user);
                break lo;
            }
            //查询用户是否存在
            for (int i = 0; i < user.size(); i++) {
                u1 = user.get(i);
                if (friendid.equals(u1.getId())) {
                    index = i;
                    break;
                } else if (i == user.size() - 1) {
                    System.out.println("无法查到该用户账号,请检查后重新输入!");
                    continue lo;
                }
            }
            //查询用户是否已经是用户的好友
            if (friends.size() != 0) {
                for (int i = 0; i < friends.size(); i++) {
                    friend = friends.get(i);
                    if (friendid.equals(friend.getId())) {
                        System.out.println("该用户已经是您的好友!");
                        continue lo;
                    }
                }
            }
            System.out.println("请输入验证信息:");
            VM = sc.next();
            //查询验证信息是否正确;
            u = user.get(index);
            if (VM.equals(u.getVM())) {
                System.out.println("验证成功!");
                System.out.println("给好友起个昵称吧~");
                name = sc.next();
                Friend friend2 = new Friend(id, friendid, VM, name);
                friends.add(friend2);
                System.out.println("添加好友成功!");
                break;
            } else {
                System.out.println("账号或者验证码不正确,请重新输入!");
                continue;
            }
        }
        /*
        //验证是否添加成功
        Friend f3 = new Friend();
        for (int i = 0; i < friends.size(); i++) {
            f3 = friends.get(i);
            System.out.println(f3.getId());
        }
        */
    }

    //建立用户删除好友界面(删除friends集合中的数据)
    public static void delFriend(String id, ArrayList<Friend> friends) {
        Scanner sc = new Scanner(System.in);
        String friendId;
        Friend friend = new Friend();
        int check = 0;
        //1.引导用户输入所需删除好友的账号
        System.out.println("请输入您所要删除的好友账号:");
        friendId = sc.next();
        //查询好友是否存在
        for (int i = 0; i < friends.size(); i++) {
            friend = friends.get(i);
            if (id.equals(friend.getUserId()) && friendId.equals(friend.getId())) {
                System.out.println("您确定要删除该好友吗?");
                System.out.println("输入1删除,输入0取消");
                check = sc.nextInt();
                if (check == 1) {
                    friends.remove(i);
                    System.out.println("删除成功!");
                    break;
                } else if (check == 0) {
                    System.out.println("操作已取消");
                    break;
                } else {
                    System.out.println("您的输入有误.操作已被取消,请重试!");
                    break;
                }
            } else if (i == friends.size() - 1) {
                System.out.println("该用户不存在或并不是您的好友!");
            }
        }
    }

    //建立用户修改好友昵称界面
    public static void alterFriend(String id, ArrayList<Friend> friends) {
        Scanner sc = new Scanner(System.in);
        String friendId;
        Friend friend = new Friend();
        String name;
        //1.引导用户输入所需修改好友的账号
        System.out.println("请输入您所要修改的好友账号:");
        friendId = sc.next();
        //查询好友是否存在
        for (int i = 0; i < friends.size(); i++) {
            friend = friends.get(i);
            if (id.equals(friend.getUserId()) && friendId.equals(friend.getId())) {
                System.out.println("请输入您要修改的昵称:");
                name = sc.next();
                friend.setName(name);
                System.out.println("修改成功!");
                break;
            } else if (i == friends.size() - 1) {
                System.out.println("该用户不存在或并不是您的好友!");
            }
        }
    }

    //建立用户查看界面
    public static void checkFriend(String id, ArrayList<Friend> friends) {
        Scanner sc = new Scanner(System.in);
        //提供两种选择,单独调出一个用户的信息或者,全部调用出来
        String friendId;
        Friend friend = new Friend();
        String name;
        System.out.println("请输入您所要查看用户的账号:(输入-1可查看所以成员信息)");
        friendId = sc.next();
        //-1直接遍历输出
        if (friendId.equals("-1")) {
            for (int i = 0; i < friends.size(); i++) {
                friend = friends.get(i);
                System.out.println("账号:" + friend.getId() + "验证信息:" + friend.getVM() + "昵称" + friend.getName());
            }
        } else {
            //查询好友是否存在
            for (int i = 0; i < friends.size(); i++) {
                friend = friends.get(i);
                if (id.equals(friend.getUserId()) && friendId.equals(friend.getId())) {
                    System.out.println("账号:" + friend.getId() + "验证信息:" + friend.getVM() + "昵称" + friend.getName());
                } else if (i == friends.size() - 1) {
                    System.out.println("该用户不存在或并不是您的好友!");
                }
            }
        }
    }

    //管理员登陆系统
    public static void enterAdmin(ArrayList<User> user) {
        Scanner sc = new Scanner(System.in);
        //引导管理员登录
        while (true) {
            System.out.println("请输入账号:");
            String id = sc.next();
            System.out.println("请输入密码:");
            String password = sc.next();
            if (id.equals("123456") && password.equals("111111")) {
                System.out.println("登陆成功,欢迎使用!");
                adminInterface(user);
                break;
            } else {
                System.out.println("账号或密码错误,请重新输入!");
                continue;
            }
        }
    }

    //建立管理员界面
    public static void adminInterface(ArrayList<User> user) {
        Scanner sc = new Scanner(System.in);
        //界面展示
        lo:
        while (true) {
            System.out.println("---------------");
            System.out.println("1.删除用户");
            System.out.println("2.查看用户");
            System.out.println("3.退出");
            String op = sc.next();
            switch (op) {
                case "1":
                    delUser(user);
                    break;
                case "2":
                    adminCheck(user);
                    break;
                case "3":
                    System.out.println("感谢您的使用!");
                    break lo;
                default:
                    System.out.println("输出错误请重新输入!");
                    break;
            }
        }
    }

    //管理员删除用户
    public static void delUser(ArrayList<User> user) {
        Scanner sc = new Scanner(System.in);
        User u = new User();
        //引导输入被删除用户的号码
        System.out.println("请输入要删除的用户账号:");
        String userId = sc.next();
        for (int i = 0; i < user.size(); i++) {
            u = user.get(i);
            if (userId.equals(u.getId())) {
                user.remove(i);
                System.out.println("删除成功!");
            }
        }
    }

    //管理员查看用户信息
    public static void adminCheck(ArrayList<User> user) {
        //同样提供两种遍历查看方式
        Scanner sc = new Scanner(System.in);
        String userId;
        User u = new User();
        String name;
        System.out.println("请输入您所要查看用户的账号:(输入-1可查看所以成员信息)");
        userId = sc.next();
        //-1直接遍历输出
        if (userId.equals("-1")) {
            for (int i = 0; i < user.size(); i++) {
                u = user.get(i);
                System.out.println("账号:" + u.getId() + "验证信息:" + u.getVM() + "密码" + u.getPassWord());
            }
        }
    }
}